#!/usr/bin/env python
import os
from glob import glob
from getmtf_7 import measure_mtf
from astropy.table import Table
import sys
import numpy as np

mtf_dir = sys.argv[1]
dark_f = sys.argv[2]
xcent_h = int(sys.argv[3])
ycent_h = int(sys.argv[4])
xsize_h = int(sys.argv[5])
ysize_h = int(sys.argv[6])

xcent_v = int(sys.argv[7])
ycent_v = int(sys.argv[8])
xsize_v = int(sys.argv[9])
ysize_v = int(sys.argv[10])

mtf_files = glob(mtf_dir+"/*.fits")
print(mtf_files)
tab = Table()
mtf_hs = np.zeros(len(mtf_files))
mtf_vs = np.zeros(len(mtf_files))
for i in range(len(mtf_files)):
    mtf_h, sigma_h = measure_mtf(mtf_files[i], fbias=dark_f, xcent=xcent_h, ycent=ycent_h, xsize=xsize_h, ysize=ysize_h, smooth=2, pixelsize=1.85, oversample=5, freq=50.0, ffig=mtf_files[i].replace('fits','png'),num_out_flag=True)
    mtf_v, sigma_v = measure_mtf(mtf_files[i], fbias=dark_f, xcent=xcent_v, ycent=ycent_v, xsize=xsize_v, ysize=ysize_v, smooth=2, pixelsize=1.85, oversample=5, freq=50.0, ffig=mtf_files[i].replace('fits','png'),num_out_flag=True)   
    print(mtf_files[i][-13:],"mtf_h =",mtf_h,"mtf_v =",mtf_v)
    mtf_hs[i] = mtf_h
    mtf_vs[i] = mtf_v     
mtf_files.append('median')
mtf_files.append('max')
mtf_files.append('min')
tab['name'] = mtf_files
tab['mtf_h'] = np.append(mtf_hs,[np.median(mtf_hs),np.max(mtf_hs),np.min(mtf_hs)])
tab['mtf_v'] = np.append(mtf_vs,[np.median(mtf_vs),np.max(mtf_vs),np.min(mtf_vs)])
tab.write("mtf_result.tab",format='ipac',overwrite=True)
